package com.example.viewtest.draw_6;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.viewtest.Utils;

/**
 * 仪表
 */
public class DashView extends View {

    private static final float PAINDING = Utils.db2px(20);
    private static final float LABEL_HEIGHT = Utils.db2px(10);
    private static final float LABEL_WIDTH = Utils.db2px(5);
    private static final int LABEL_COUNT = 24;
    private static final int ANGLE = 120;

    private float value = 0;

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private RectF rectF = new RectF();
    private PathDashPathEffect effect;

    private Path pathScaleArc = new Path();
    private Path pathScale = new Path();
    private PathMeasure pathMeasure = new PathMeasure();
    private float offsetScaleAngel;

    {
        pathScale.addRect(0, 0, LABEL_WIDTH, LABEL_HEIGHT, Path.Direction.CW);
    }

    public DashView(Context context) {
        super(context);
    }

    public DashView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DashView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        rectF.set(PAINDING, PAINDING, getMeasuredWidth() - PAINDING, getMeasuredWidth() - PAINDING);
        pathScaleArc.reset();
        pathScaleArc.addArc(rectF, 90 + ANGLE / 2, 360 - ANGLE);
        pathMeasure.setPath(pathScaleArc, false);

        float step = (pathMeasure.getLength() - LABEL_WIDTH) / LABEL_COUNT;
        effect = new PathDashPathEffect(pathScale, step, 0, PathDashPathEffect.Style.ROTATE);

        offsetScaleAngel = LABEL_WIDTH / pathMeasure.getLength() * (360 - ANGLE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(Utils.db2px(1));

        paint.setPathEffect(effect);
        canvas.drawPath(pathScaleArc, paint);
        paint.setPathEffect(null);
        canvas.drawPath(pathScaleArc, paint);

        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(rectF.centerX(), rectF.centerY(), LABEL_HEIGHT / 2, paint);

        paint.setColor(Color.RED);
        canvas.rotate(90 + ANGLE / 2 + (value / LABEL_COUNT) * (360 - ANGLE) + offsetScaleAngel * (0.5f - value / LABEL_COUNT), rectF.centerX(), rectF.centerY());
        canvas.drawLine(rectF.centerX() + LABEL_HEIGHT / 2 + 3, rectF.centerY(),
                rectF.centerX() + rectF.width() / 2 - LABEL_HEIGHT - 3, rectF.centerY(), paint);
    }


    public void setValue(float val) {
        this.value = val % (LABEL_COUNT+1);
        invalidate();
    }
}
